package com.haiyou.data.common.persister.mysql.help;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;

import com.haiyou.common.entity.Entity;
import com.haiyou.common.util.object.ClassUtils;
import com.haiyou.data.common.persister.mysql.meta.ColumnMeta;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ColumnHelper {

	static public Class<?> getEntityClass(ColumnMeta columnMeta) {
//		try {
//			return (Class<?>) Class.forName(columnMeta.getEntityClassName());
//		} catch (ClassNotFoundException e) {
//			throw new NullPointerException(columnMeta.getEntityClassName());
//		}
		return columnMeta.getEntityClass();
	}

	static public Field getField(ColumnMeta columnMeta) {
//		return ClassUtils.findDeclaredField(true, getEntityClass(columnMeta), columnMeta.getFieldName());
		return columnMeta.getField();
	}

	static public Type getGenericType(ColumnMeta columnMeta) {
//		return getField(columnMeta).getGenericType();
		return columnMeta.getField().getGenericType();
	}

	static public Method getSetter(ColumnMeta columnMeta) {
//		return ClassUtils.findDeclaredMethod(true, getEntityClass(columnMeta), columnMeta.getSetter(),
//				getField(columnMeta).getType());
		return columnMeta.getSetterMethod();
	}

	static public Method getGetter(ColumnMeta columnMeta) {
		//return ClassUtils.findDeclaredMethod(true, getEntityClass(columnMeta), columnMeta.getGetter());
		return columnMeta.getGetterMethod();
	}

	/**
	 * 转换成持久化的值
	 * 
	 * @param columnMeta
	 * @param entity
	 * @return
	 */
	public static Object cast(ColumnMeta columnMeta, Entity<?> entity) {
		Object val = null;

		try {
			val = getGetter(columnMeta).invoke(entity);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage() + ":" + columnMeta + ",\n" + entity, e);
		}

		if (val == null) {
			return val;
		}

		String valClass = val.getClass().getTypeName();
		if (valClass.contains("Concurrent") || valClass.contains("Blocking") || valClass.contains("SetFrom")) {
			log.debug("{} 是并发安全对象，可使用非线程安全对象替换", columnMeta.getFieldName());
		}

		if (ClassUtils.findDeclaredField(true, getEntityClass(columnMeta), columnMeta.getFieldName())
				.getGenericType() != String.class && columnMeta.getColumnType().contains("text")) {// 引用类型

			if (!columnMeta.isCustomize()) {
				val = com.alibaba.fastjson2.JSON.toJSONString(val);
			} else {
				// 如果自定义序列化已经通过调用getter转换成json-string
			}

		} else if (val instanceof Boolean) {

			val = ((Boolean) val) ? 1 : 0;

		} else if (val.getClass() == boolean.class) {

			val = ((boolean) val) ? 1 : 0;

		} else if (val instanceof BigInteger) {

			// ((BigInteger) val).longValueExact();

		} else if (val instanceof BigDecimal) {

			// ((BigDecimal) val).longValueExact();

		}

		else if (val instanceof java.time.LocalDateTime) {

		} else if (val instanceof java.util.Date) {

			val = java.time.LocalDateTime.ofInstant(((java.util.Date) val).toInstant(),
					java.time.ZoneId.systemDefault());

		} else if (val instanceof java.sql.Date) {

			val = ((java.sql.Date) val).toLocalDate().atTime(java.time.LocalTime.now(java.time.ZoneId.systemDefault()));

		} else if (val instanceof java.sql.Timestamp) {

			val = ((java.sql.Timestamp) val).toLocalDateTime();

		}

		return val;
	}

}
